<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
if($_RQ['op']=='get'){
	$account=pdo_get('core_accounts',array('uniacid'=>$_SESSION['uniacid']),array('title','desc','logo','groupid','attachment','credit2','endtime','opendiys'));
	if($account['endtime'] && $account['endtime'] < TIMESTAMP+15*24*3600 && !$isfounder){
		$status=2;//将过期
	}
	if($account['endtime'] && $account['endtime'] < TIMESTAMP && !$isfounder){
		$status=3;//已过期
	}
	$_SESSION['account']['groupid']=$account['groupid'];
	$_SESSION['account']['endtime']=$account['endtime'];
	$_SESSION['account']['status']=$status;
	$account['url']=custom_url('core/home.welcome/',true);
	$group=pdo_get('core_account_group',array('id'=>$account['groupid']),array('id','title','access','resource','opendiys'));
	if(!$account['opendiys'] && $group['opendiys']){
		pdo_update('core_accounts',array('opendiys'=>$group['opendiys']),array('uniacid'=>$_SESSION['uniacid']));
		$account['opendiys']=$group['opendiys'];
	}
	$default_group=pdo_getcolumn('core_account_group',array('isdefault'=>1),'id');
	if($group && $group['id']==$default_group){
		$group['title'].='（默认套餐）';
	}
	$group['title']=$group['title']?$group['title']:'未使用套餐';
	$group['access']=$group['access']?$group['access']:'不限制';
	if($group['resource'] && $account['attachment']){//扩容部分
		$group['resource'] +=$account['attachment']*1024;
	}
	$group['resource']=$group['resource']?$group['resource']:'不限制';
	$return['group']=$group;
	$account['endtime']=$account['endtime']?date('Y-m-d',$account['endtime']):'长期有效';
	$count=array();
	$time=mktime(0,0,0,date('m'));
	//exi('时间：'.$time,'error');
	$count['new_member']=pdo_count('core_members',array('uniacid'=>$_SESSION['uniacid'],'createtime >'=>$time));
	$count['all_member']=pdo_count('core_members',array('uniacid'=>$_SESSION['uniacid']));
	$time=mktime(0,0,0,date('m'),date('d'));
	$count['new_access']=pdo_count('core_access_log',array('uniacid'=>$_SESSION['uniacid'],'this'=>2,'createtime >'=>$time));
	$count['all_access']=pdo_count('core_access_log',array('uniacid'=>$_SESSION['uniacid'],'this'=>2));
	$return['count']=$count;
	$return['item']=$account;
	/*$return['htmls']=array(
				array('type'=>'list_panel','width'=>'50%','title'=>'站点信息_siteinfo','datas'=>array(
					array('title'=>'站点名称','num'=>1,'value'=>$return['item']['title'],'help'=>'站点全局简称'),
					array('title'=>'站点地址','num'=>1,'value'=>$return['item']['url'],'help'=>'站点全局域名地址'),
					array('title'=>'站点余额','num'=>1,'value'=>$return['item']['credit2'],'help'=>'站点剩余的账号余额'),
				)),
				array('type'=>'list_panel','width'=>'50%','title'=>'套餐信息_packageinfo','datas'=>array(
					array('title'=>'套餐名称','value'=>$return['group']['title'],'help'=>'当前使用套餐名称'),
					array('title'=>'套餐有效期','value'=>$account['endtime'],'help'=>''),
					array('title'=>'套餐附件容量','value'=>$return['group']['resource'],'help'=>'套餐附件容量'),
				)),
				array('type'=>'data','num'=>12,'one_num'=>3,'title'=>'用户数据/访问数据_user data/access data','datas'=>array(
					array('title'=>'本月新增会员','num'=>1,'data'=>$count['new_member']),
					array('title'=>'总会员数','num'=>1,'data'=>$count['all_member']),
					array('title'=>'当日访问量','num'=>1,'data'=>$count['new_access']),
					array('title'=>'总访问量','num'=>1,'data'=>$count['all_access']),
				)),
				
	);
	if(empty($account['desc']) && empty($account['logo'])){
		$return['htmls'][]=array('type'=>'intro','title'=>'新手引导','datas'=>array(
					array('element'=>'#top_nav_item_1','intro'=>'设置站点信息，就等于设置全网！</br>一个用户就是一个全网(包含：PC端,手机端,微信公众号,小程序,APP等)。','position'=>''),
					array('element'=>'#left_menu_1_0','intro'=>'点击基本设置，设置全网基本信息！同理可以完善：文章，产品等','position'=>''),
					array('element'=>'#left_menu_2_1','intro'=>'进入参数设置，可以设置：支付，短信，域名等参数！','position'=>''),
					array('element'=>'#top_nav_item_2','intro'=>'设置好全网基本信息后，点击应用端，完善不同的应用端(PC端,手机端,微信公众号,小程序,APP等)！','position'=>''),
		));
	}*/
	if($_SESSION['user']['type']==3){
		$default_group=pdo_getcolumn('core_account_group',array('isdefault'=>1),'id');
		if($_SESSION['account']['groupid'] && $_SESSION['account']['groupid']!=$default_group){
			//已有套餐且非默认套餐时，不允许购买新套餐
			$tabs=array(
				array('op'=>'get','title'=>'站点信息'),
				array('op'=>'change','title'=>'更换套餐'),
				array('op'=>'more','title'=>'续费套餐'),
			);
		}else{
			$tabs=array(
				array('op'=>'get','title'=>'站点信息'),
				array('op'=>'buy','title'=>'购买套餐'),
			);
		}
		
	}else{
		$tabs=array(
			array('op'=>'get','title'=>'站点信息_siteinfo'),
		);
	}
	set_html('tabs',$tabs);
	$intypes=get_plugin_intypes();
	$inlist=array();
	foreach($intypes as $k=>&$inv){
			if($k){
				$inlist[$k]['name']='core';
				$inlist[$k]['title']=$inv;
				$inlist[$k]['action']='/account.apphome//type='.$k;
			}else{
				$inlist[18]['name']='core';
				$inlist[18]['title']=$inv;
				$inlist[18]['action']='/account.apphome//type=0';
			}
			
	}
	$return['groups']=$inlist;
	$return['diys']=array(
		array('id'=>'wxapp','title'=>'微信小程序','icon'=>'fa-weixin','color'=>'#63BE72','price'=>$app_prices['wxapp']),
		array('id'=>'ttapp','title'=>'头条/抖音小程序','icon'=>'fa-pinterest','color'=>'#FF547B','price'=>$app_prices['ttapp']),
		array('id'=>'aliapp','title'=>'支付宝小程序','icon'=>'fa-facebook-square','color'=>'#4491F1','price'=>$app_prices['aliapp']),
		array('id'=>'baiduapp','title'=>'百度小程序','icon'=>'fa-behance-square','color'=>'#7783EA','price'=>$app_prices['baiduapp']),
		array('id'=>'qqapp','title'=>'QQ小程序','icon'=>'fa-qq','color'=>'#FF4444','price'=>$app_prices['qqapp']),
		array('id'=>'pc','title'=>'PC官网建站','icon'=>'fa-windows','color'=>'#FCC600','price'=>$app_prices['pc']),
		array('id'=>'xiu','title'=>'微场景','icon'=>'fa-dropbox','color'=>'#C3A769','price'=>$app_prices['xiu']),
		array('id'=>'h5game','title'=>'互动营销','icon'=>'fa-google-plus','color'=>'#FF547B','price'=>$app_prices['h5game']),
	);
	$return['params']=array('type'=>-1);
	$return['isgroup']=intval($_RQ['isgroup']);
	$return['extradata']=array();
	exi($return);
}
/*
if($_RQ['op']=='buy'){//本op已弃用
	//购买套餐。选择套餐、选择时长。
	$default_group=pdo_getcolumn('core_account_group',array('isdefault'=>1),'id');
	if($_SESSION['account']['groupid'] && $_SESSION['account']['groupid']!=$default_group){
		//已有套餐且非默认套餐时，不允许购买新套餐
		exi('您已购买套餐，如需调整请更换套餐！','error','change');
	}
	$credit=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'credit2');
	if($_RQ['postdata']){
		$post=get_postdata();
		$group_id=intval($post['group_id']);
		if(!$group_id){
			exi('请先选择要购买的套餐！','error');
		}
		$group=pdo_get('core_account_group',array('id'=>$group_id),array('id','days','price'));
		if(!$group){
			exi('指定套餐不存在！请更换！','error');
		}
		$num=intval($post['num']);
		$fee=$num*$group['price'];
		if($fee>$credit){
			exi('余额不足！请先充值！','error','core/account.recharge/');
		}
		$update=array(
			'groupid'=>$post['group_id'],
			'endtime'=>TIMESTAMP+$group['days']*$num*86400,
		);
		$pay=array(
			'type'=>2,
			'is_add'=>0,
			'fee'=>$fee,
			'remark'=>"购买套餐【{$group['title']}】",
		);
		model('core/web')->account_credit($pay);
		pdo_update('core_accounts',$update,array('uniacid'=>$_SESSION['uniacid']));
		$_SESSION['account']['groupid']=$post['group_id'];
		exi('套餐购买成功！','','get');
	}
	$groups=pdo_getall('core_account_group',array(),array('id','title','price','days'),'id');
	$group_radios=array();
	
	foreach($groups as $key=>&$group){
		if($default_group==$group['id']){
			unset($groups[$key]);
			continue;
		}
		$group['title']="{$group['title']}（{$group['price']}元/{$group['days']}天）";
		$group['options']=array();
		for($i=1;$i<6;$i++){
			$group['options'][$i]=($i*$group['days']).'天';
			if($credit<$group['price']*$i){
				$group['options'][$i].='（余额不足）';
			}
		}
		$group_radios[$group['id']]=$group['title'];
	}
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'购买套餐',
		'edit'=>array(
			array('type'=>'radio','name'=>'group_id','title'=>'选择要购买的套餐','options'=>'group_radios'),
			array('type'=>'radio','name'=>'num','title'=>'选择购买时长','options'=>'groups[pagedata.item.group_id].options'),
		),
	);
	$return['group_radios']=$group_radios;
	$return['list']=$groups;
	$tabs=array(
		array('op'=>'get','title'=>'站点信息'),
		array('op'=>'buy','title'=>'购买套餐'),
	);
	set_html('tabs',$tabs);
	exi($return);
}
if($_RQ['op']=='change'){
	//更换套餐。如何更换？变更套餐时，结合当前套餐剩余天数和两个套餐价格比例，调整天数。
	//显示当前套餐和剩余天数，然后显示可供更换的套餐
	$default_group=pdo_getcolumn('core_account_group',array('isdefault'=>1),'id');
	if(!$_SESSION['account']['groupid'] || $_SESSION['account']['groupid']==$default_group){
		exi('请先购买套餐！','error','buy');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$new_group=intval($post['new_group']);
		if(!$new_group){
			exi('请先选择要更换的套餐~','error');
		}
		$group=pdo_get('core_account_group',array('id'=>$new_group));
		if(!$group){
			exi('指定套餐不存在~','error');
		}
		$current_group=pdo_get('core_account_group',array('id'=>$_SESSION['account']['groupid']));
		$current_days=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'endtime');
		$current_days=$current_days-TIMESTAMP;
		$new_endtime=TIMESTAMP+intval(($current_group['price']*$group['days']*$current_days)/($group['price']*$current_group['days']));
		$update=array(
			'groupid'=>$group['id'],
			'endtime'=>$new_endtime,
		);
		pdo_update('core_accounts',$update,array('uniacid'=>$_SESSION['uniacid']));
		$_SESSION['account']['groupid']=$group['id'];
		exi('套餐更换成功！','','get');
	}
	$current_group=pdo_get('core_account_group',array('id'=>$_SESSION['account']['groupid']));
	$current_days=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'endtime');
	$current_endtime=$current_days?date('Y-m-d',$current_days):'长期有效';
	$current_days=$current_days?$current_days-TIMESTAMP:86400*100;//长期套餐更换时自动这算为100天
	$groups=pdo_getall('core_account_group',array(),array('id','title','price','days'));
	foreach($groups as $key=>&$group){
		if($group['id']==$current_group['id'] || $group['id']==$default_group || !$group['days']){
			unset($groups[$key]);
			continue;
		}
		
		$endtime=date('Y-m-d',TIMESTAMP+intval(($current_group['price']*$group['days']*$current_days)/($group['price']*$current_group['days'])));
		$group['title']="{$group['title']}（{$group['price']}元/{$group['days']}天）更换后有效期至：{$endtime}";
	}
	$return['item']=array(
		'current_title'=>$current_group['title'],
		'current_endtime'=>$current_endtime,
	);
	$return['groups']=$groups;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'更换套餐',
		'edit'=>array(
			array('type'=>'string','name'=>'current_title','title'=>'当前套餐','read'=>true),
			array('type'=>'string','name'=>'current_endtime','title'=>'当前套餐有效期','read'=>true),
			array('type'=>'select','name'=>'new_group','title'=>'新套餐','options'=>'groups'),
		),
	);
	$tabs=array(
		array('op'=>'get','title'=>'站点信息'),
		array('op'=>'change','title'=>'更换套餐'),
		array('op'=>'more','title'=>'续费套餐'),
	);
	set_html('tabs',$tabs);
	exi($return);
}
if($_RQ['op']=='more'){
	//续费套餐。选择时长
	if(!$_SESSION['account']['groupid'] || $_SESSION['account']['groupid']==$default_group){
		exi('请先购买套餐！','error','buy');
	}
	$group=pdo_get('core_account_group',array('id'=>$_SESSION['account']['groupid']));
	if($group['no_renew']){
		exi('该套餐不支持续费！','error');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$num=intval($post['num']);
		if(!$num){
			exi('请先选择续费时长~','error');
		}
		$fee=$group['price']*$num;
		$credit=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'credit2');
		if($credit<$fee){
			exi('余额不足~','error');
		}
		$pay=array(
			'type'=>2,
			'is_add'=>0,
			'fee'=>$fee,
			'remark'=>"续费套餐【{$group['title']}】",
		);
		model('core/web')->account_credit($pay);
		$current_days=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'endtime');
		$start_time=$current_days>TIMESTAMP?$current_days:TIMESTAMP;
		$new_endtime=$start_time+$num*$group['days']*86400;
		$update=array(
			'endtime'=>$new_endtime,
		);
		pdo_update('core_accounts',$update,array('uniacid'=>$_SESSION['uniacid']));
		exi('套餐续费成功！','','get');
	}
	$current_group=pdo_get('core_account_group',array('id'=>$_SESSION['account']['groupid']));
	$current_days=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'endtime');
	if(!$current_days){
		exi('您的套餐长期有效，无需续费！','error','get');
	}
	$current_endtime=date('Y-m-d',$current_days);
	$options=array();
	$credit=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'credit2');
	for($i=1;$i<6;$i++){
		$option=array(
			'id'=>$i,
			'title'=>($i*$group['days']).'天',
		);
		if($credit<$group['price']*$i){
			$option['title'].='（余额不足）';
		}
		$options[]=$option;
	}
	$return['options']=$options;
	$return['item']=array(
		'current_title'=>$current_group['title'],
		'current_endtime'=>$current_endtime,
	);
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'更换套餐',
		'edit'=>array(
			array('type'=>'string','name'=>'current_title','title'=>'当前套餐','read'=>true),
			array('type'=>'string','name'=>'current_endtime','title'=>'当前套餐有效期','read'=>true),
			array('type'=>'select','name'=>'num','title'=>'续费','options'=>'options'),
		),
	);
	$tabs=array(
		array('op'=>'get','title'=>'站点信息'),
		array('op'=>'change','title'=>'更换套餐'),
		array('op'=>'more','title'=>'续费套餐'),
	);
	set_html('tabs',$tabs);
	exi($return);
}*/
if($_RQ['op']=='group_item'){
	//套餐详情
	$app_prices=core_setting('app_price');
	if($_RQ['postdata']){
		$post=get_postdata();
		$opendiys=explode(';',$post['opendiys']);
		$oldopendiys=explode(';',$post['oldopendiys']);
		$fee=0;
		foreach($opendiys as $k=>&$vp){
			if(!empty($vp)){
				foreach($oldopendiys as $vo){
					if($vp==$vo){
						unset($opendiys[$k]);
					}
				}
			}
		}
		foreach($opendiys as $k=>&$vp){
			$fee +=$app_prices[$vp];
		}
		$order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'opendiys'=>$post['opendiys'],
			'price'=>$fee,
			'ordersn'=>get_ordersn('core_account_order','ordersn','DIY'),
			'createtime'=>TIMESTAMP,
			'title'=>'平台功能开通',
			'status'=>0,
		);
		pdo_insert('core_account_order',$order);
		$order_id=pdo_insertid();
		$pay=array(
			'id'=>$order_id,
			'title'=>'平台功能开通',			
			'fee'=>$order['price'],
			'back_url'=>'core/account.siteinfo/group_payresult',
		);
		$pay_id=cfc('core')->account_pay($pay);
		pdo_update('core_account_order',array('pay_id'=>$pay_id),array('id'=>$order_id));
		exi('','','core/account.pay/main/id='.$pay_id);
	}
	$account=pdo_get('core_accounts',array('uniacid'=>$_SESSION['uniacid']),array('title','desc','logo','groupid','credit2','endtime','opendiys'));
	$group=pdo_get('core_account_group',array('id'=>$account['groupid']),array('id','title','access','resource','logo'));
	$default_group=pdo_getcolumn('core_account_group',array('isdefault'=>1),'id');
	if($group && $group['id']==$default_group){
		$group['title'].='（默认套餐）';
	}
	$group['title']=$group['title']?$group['title']:'未使用套餐';
	$group['access']=$group['access']?$group['access']:'不限制';
	$group['resource']=$group['resource']?$group['resource']:'不限制';
	$group['endtime']=$account['endtime']?date('Y-m-d',$account['endtime']):'长期有效';
	$group['opendiys']=$group['oldopendiys']=$account['opendiys'];
	$help='按需选择要开通的功能！';
	if($_SESSION['isfounder']){
			$help='<em>管理员身份：默认全部开通，无需设置</em>';
	}
	$return['item']=$group;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			//array('type'=>'image','name'=>'logo','title'=>'套餐LOGO','read'=>true,'help'=>""),
			array('type'=>'string','name'=>'title','title'=>'套餐名称','read'=>true,'help'=>""),
			array('type'=>'string','name'=>'endtime','title'=>'套餐有效期','read'=>true,'help'=>""),
			array('type'=>'string','name'=>'resource','title'=>'套餐附件容量','read'=>true,'help'=>""),
			array('type'=>'checkbox_buy','name'=>'opendiys','title'=>'开启的DIY功能','read'=>true,'options'=>'diys','help'=>$help),
		),
	);
	$return['diys']=array(
		array('id'=>'wxapp','title'=>'微信小程序','icon'=>'fa-weixin','color'=>'#63BE72','price'=>$app_prices['wxapp']),
		array('id'=>'ttapp','title'=>'头条/抖音小程序','icon'=>'fa-pinterest','color'=>'#FF547B','price'=>$app_prices['ttapp']),
		array('id'=>'aliapp','title'=>'支付宝小程序','icon'=>'fa-facebook-square','color'=>'#4491F1','price'=>$app_prices['aliapp']),
		array('id'=>'baiduapp','title'=>'百度小程序','icon'=>'fa-behance-square','color'=>'#7783EA','price'=>$app_prices['baiduapp']),
		array('id'=>'qqapp','title'=>'QQ小程序','icon'=>'fa-qq','color'=>'#FF4444','price'=>$app_prices['qqapp']),
		array('id'=>'pc','title'=>'PC官网建站','icon'=>'fa-windows','color'=>'#FCC600','price'=>$app_prices['pc']),
		array('id'=>'xiu','title'=>'微场景','icon'=>'fa-dropbox','color'=>'#C3A769','price'=>$app_prices['xiu']),
		array('id'=>'h5game','title'=>'互动营销','icon'=>'fa-google-plus','color'=>'#FF547B','price'=>$app_prices['h5game']),
	);
	foreach($return['diys'] as &$item){
		if(strpos($group['opendiys'],';'.$item['id'].';') !== false){
			$item['read']=true;
		}else{
			$item['read']=false;
		}
	}
	exi($return);
}
if($_RQ['op']=='group_payresult'){
	if(!$_SESSION['payresult']){
		exi('参数错误！','error','log');
	}
	$payresult=$_SESSION['payresult'];
	unset($_SESSION['payresult']);
	$order=pdo_get('core_account_order',array('id'=>$payresult['id']));
	if(!$order){
		exi('订单不存在！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付出错，订单金额不匹配！','error','log');
	}
	if($order['status']==0){
		$update=array(
			'opendiys'=>$order['opendiys'],
		);
		pdo_update('core_accounts',$update,array('uniacid'=>$_SESSION['uniacid']));
		$update=array(
			'status'=>1,
			'pay_type'=>$payresult['pay_type'],
		);
		pdo_update('core_account_order',$update,array('id'=>$order['id']));
	}
	exi('购买成功！','','core/account.siteinfo/');
}
if($_RQ['op']=='buy_attachment'){
	//附件扩容
	$setting=core_setting('num_price');
	if(!$setting['attachment']){
		exi('系统暂不支持扩容，请联系管理员！','error');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		$num=intval($post['num']);
		if(!$num){
			exi('请输入扩容容量！','error');
		}
		$fee=$num*$setting['attachment'];
		$attachment_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'num'=>$num,
			'price'=>$fee,
			'ordersn'=>get_ordersn('core_account_attachment_order','ordersn','AT'),
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		pdo_insert('core_account_attachment_order',$attachment_order);
		$order_id=pdo_insertid();
		$pay=array(
			'id'=>$order_id,
			'title'=>'套餐购买',
			
			'fee'=>$attachment_order['price'],
			'back_url'=>'core/account.siteinfo/attachment_payresult',
		);
		$pay_id=cfc('core')->account_pay($pay);
		pdo_update('core_account_attachment_order',array('pay_id'=>$pay_id),array('id'=>$order_id));
		exi('','','core/account.pay/main/id='.$pay_id);
		/*
			购买的扩容附件如何保存？
			是否在core_accounts里面加个字段保存购买容量？
			还是新建一张表以保存日志？
			加字段简单点，多次购买时把总数存在该字段即可。读取用户附件容量时，直接把套餐内置容量加上该字段值，即可得到总的附件容量！
		*/
		
	}
	
	$item=array('num'=>1);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'unitstring','name'=>'num','title'=>'购买数量','unit'=>'G','help'=>"请输入整数！单价：{$setting['attachment']}元/G"),
		),
	);
	exi($return);
}
if($_RQ['op']=='attachment_payresult'){
	if(!$_SESSION['payresult']){
		exi('参数错误！','error','log');
	}
	$payresult=$_SESSION['payresult'];
	unset($_SESSION['payresult']);
	$order=pdo_get('core_account_attachment_order',array('id'=>$payresult['id']));
	if(!$order){
		exi('订单不存在！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付出错，订单金额不匹配！','error','log');
	}
	if($order['status']==0){
		$update=array(
			'attachment +='=>$order['num'],
		);
		pdo_update('core_accounts',$update,array('uniacid'=>$_SESSION['uniacid']));
		$update=array(
			'status'=>1,
			'pay_type'=>$payresult['pay_type'],
		);
		pdo_update('core_account_attachment_order',$update,array('id'=>$order['id']));
	}
	exi('扩容成功！','','core/account.siteinfo/');
}
if($_RQ['op']=='bind_openfans'){
	//绑定粉丝
	if($_RQ['postdata']){
		$post=get_postdata();
		$name = trim($post['name']);
		pdo_delete('core_user_failed_login',array('lastupdate <'=>TIMESTAMP-900));
		$failed = pdo_get('core_user_failed_login', array('name' => $name));
		if ($failed['count'] >= 5){
			exi('输入密码错误次数超过5次，请在15分钟后再登录','error');
		}
		if(empty($name)) {
			exi('请输入要登录的用户名','error');
		}
		if(empty($post['password'])) {
			exi('请输入密码','error');
		}
		$record = pdo_get('core_users',array('name'=>$name),array('id','salt','password'));
		if(empty($record)){
			exi('账号不存在！','error');
		}else{
			if(!check_password($post['password'],$record['salt'],$record['password'])){
				if (empty($failed)) {
					pdo_insert('core_user_failed_login', array( 'name' => $name, 'count' => '1', 'lastupdate' => TIMESTAMP));
				} else {
					pdo_update('core_user_failed_login', array('count' => $failed['count'] + 1, 'lastupdate' => TIMESTAMP), array('id' => $failed['id']));
				}
				exi('账号不存在或密码错误！','error');
			}
		}
		pdo_update('core_open_user',array('open_uid'=>'old_'.$_SESSION['openid']),array('open_id'=>'1003','uniacid'=>$_SESSION['uniacid']));
		$uniacid=pdo_getcolumn('core_open_user',array('uniacid'=>$record['id']),'uniacid');
		if($uniacid){
			pdo_update('core_open_user',array('open_uid'=>$_SESSION['openid']),array('open_id'=>'1003','uniacid'=>$record['id']));
		}else{
			$user=array(
					'uniacid'=>$record['id'],
					'open_id'=>'1003',
					'open_uid'=>$_SESSION['openid'],
			);
			pdo_insert('core_open_user',$user);
		}
		cfc('user')->login($record['id']);
		exi('站点绑定成功！','','core/account.welcome/');
	}
	$return['item']=array();
	$return['item']['nowname']=pdo_getcolumn('core_users',array('id'=>$_SESSION['uniacid']),'name');
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'copy','name'=>'nowname','title'=>'当前绑定'),
			array('type'=>'string','name'=>'name','title'=>'用户名'),
			array('type'=>'password','name'=>'password','title'=>'登录密码'),
		),
	);
	exi($return);
}
if($_RQ['op']=='group_list'){
	//获取套餐列表
	$list=pdo_getall('core_account_group',array(),array('id','title','price','plugin_name','logo','desc'));
	foreach($list as &$item){
		if($item['plugin_name']){
			$item['intype']=pdo_getcolumn('core_plugins',array('name'=>$item['plugin_name']),'intype');
		}
	}
	exi($list);
}
if($_RQ['op']=='detail'){
	if($_RQ['postdata']){
		if($_SESSION['isfounder']){
			exi('您是超级管理员，无需购买！','error');
		}
		$post=get_postdata();
		$id=intval($post['id']);
		$num=intval($post['num']);
		$num=1;//暂时默认为1，不用用户前端选择
		$item=pdo_get('core_account_group',array('id'=>$id));		
		//如何区分是直接购买还是购买规格？
		if($post['is_option']){
			//购买规格
			$options=json_decode($item['options'],true);
			if(!$options || !$options[$post['option_id']]){
				exi('找不到指定套餐规格！','error');
			}
			$option=$options[$post['option_id']];
			$days=array(
				'1'=>1,
				'2'=>30,
				'3'=>365,
			);
			$day=$days[$option['time_type']]*$option['num'];
			$price=$option['price'];
		}else{
			$day=365;
			$price=$item['price'];
		}
		$fee=$price*$num;	
		$buy_type=$_SESSION['account']['groupid']==$id?'续费':'购买';
		$account_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'groupid'=>$id,
			'num'=>$num,
			'day'=>$day,
			'ordersn'=>get_ordersn('core_account_order','ordersn','JY'),
			'createtime'=>TIMESTAMP,
			'price'=>$fee,
			'title'=>"套餐{$buy_type}【{$num}*{$day}天】",
			'status'=>0,
		);
		pdo_insert('core_account_order',$account_order);
		$order_id=pdo_insertid();
		$pay=array(
			'id'=>$order_id,
			'title'=>'套餐购买',
			'fee'=>$account_order['price'],
			'back_url'=>'core/account.siteinfo/payresult/',
			'success_url'=>'core/account.myorder/',
			'error_url'=>'core/account.myorder/',
		);
		$pay_id=cfc('core')->account_pay($pay);
		pdo_update('core_account_order',array('pay_id'=>$pay_id),array('id'=>$order_id));
		exi($pay_id);
		//exi('','','core/account.pay/main/id='.$pay_id);
	}
	$id=intval($_RQ['id']);
	$plugin_name=$_RQ['plugin_name'];
	if($id){
		$item=pdo_get('core_account_group',array('id'=>$id));
	}else{
		$item=pdo_get('core_account_group',array('plugin_name'=>$plugin_name));
	}
	if(!$item){
		exi('指定套餐不存在~','error');
	}
	$item['options']=$item['options']?json_decode($item['options'],true):array();
	$plugininfo=pdo_get('core_plugins',array('name'=>$item['plugin_name']));
	$item['desc']=$item['desc']?$item['desc']:$plugininfo['description'];
	$return['item']=$item;
	$return['account']=pdo_get('core_accounts',array('uniacid'=>$_SESSION['uniacid']),array('groupid','endtime'));
	$return['account']['groupname']=pdo_getcolumn('core_account_group',array('id'=>$return['account']['groupid']),'title');
	//购买按钮，如果是已购买则显示续费，如果是第一次购买，则显示购买
	if($_SESSION['account']['groupid']==$id){
		$return['buy_button']='续费';
	}else{
		$return['buy_button']='购买';
	}
	$return['post']=array(
		'id'=>$item['id'],
	);
	$return['time_types']=array(
		'1'=>'天',
		'2'=>'月',
		'3'=>'年',
	);
	$return['price']=$item['price'];
	exi($return);
}
if($_RQ['op']=='payresult'){
	//支付结果处理
	if(!$_SESSION['payresult']){
		exi('参数错误！','error','log');
	}
	$payresult=$_SESSION['payresult'];
	unset($_SESSION['payresult']);
	$account_order=pdo_get('core_account_order',array('id'=>$payresult['id']));
	if(!$account_order){
		exi('订单不存在！','error');
	}
	if($account_order['price']!=$payresult['fee']){
		exi('支付出错，订单金额不匹配！','error','log');
	}
	if($account_order['status']==0){
		$accounts=pdo_get('core_accounts',array('uniacid'=>$account_order['uniacid']),array('groupid','endtime'));
		$current_days=$accounts['endtime'];
		$oldgroupid=$accounts['groupid'];
		$start_time=$current_days>TIMESTAMP?$current_days:TIMESTAMP;
		$new_endtime=$start_time+$account_order['num']*$account_order['day']*86400;
		$update=array(
			'endtime'=>$new_endtime,
			'groupid'=>$account_order['groupid'],
		);
		pdo_update('core_accounts',$update,array('uniacid'=>$account_order['uniacid']));
		$update=array(
			'status'=>1,
			'pay_type'=>$payresult['pay_type'],
		);
		pdo_update('core_account_order',$update,array('id'=>$account_order['id']));
	}else{
		exi('订单已经支付！','error');
	}
	model('core/jinyun_socket')->send2uid(array('paymid'=>$_SESSION['uid']),$account_order['uniacid']);
	if($oldgroupid!=$account_order['groupid']){
		//如果是购买套餐，需要更新当前用户权限，并跳转到套餐首页，如果套餐有首页的话
		model('core/user')->update_permission();
		$url=$item['index_url']?:'core/account.siteinfo/';
		exi('套餐购买成功！','',$url);
	}
	exi("套餐续费成功！",'','core/account.siteinfo/');
}